//
//  ForgetPasswordController.swift
//  SCA
//
//  Created by tjun on 2025/9/14.
//

import UIKit
import TTTAttributedLabel


class ForgetPasswordController: BaseController {

    private let usernameTextField = LabeledTextField(tip: "用户名 至少6个字符，必须由字母和数字所组合。".localizedString)
    private let passwordTextField = LabeledTextField(tip: "密码 至少8个字符，必须由字母,数字和特殊字符组合。".localizedString)
    private let comfirmPasswordTextField = LabeledTextField(tip: "请重新输入您的密码。".localizedString)
    private let otpTextField = LabeledTextField()
    private let confirmButton = MainButton(type: .system, title: "确认".localizedString, font: UIFontMake(16), textColor: .white)

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
 
    private func setupUI() {
        
        navigationBar.title = "忘记密码".localizedString
        
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: navBarHeight, width: view.width, height: screenHeight - navBarHeight)).addTo(view)
        scrollView.keyboardDismissMode = .onDrag
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.alwaysBounceVertical = true
        scrollView.contentInsetAdjustmentBehavior = .never
        
        let contentSize = CGSize(width: scrollView.width, height: scrollView.height)
        scrollView.contentSize = contentSize
        
        let contentView = UIView().addTo(scrollView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.size.equalTo(contentSize)
        }

        usernameTextField.titleAttributedText = "用户名*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        usernameTextField.textField.keyboardType = .asciiCapable
        usernameTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        contentView.addSubview(usernameTextField)
        usernameTextField.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
        }
        
        passwordTextField.titleAttributedText = "密码*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        passwordTextField.textField.keyboardType = .asciiCapable
        contentView.addSubview(passwordTextField)
        passwordTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        passwordTextField.snp.makeConstraints { make in
            make.top.equalTo(usernameTextField.snp.bottom).offset(20)
            make.left.right.equalTo(usernameTextField)
        }

        comfirmPasswordTextField.titleAttributedText = "确认密码*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        comfirmPasswordTextField.textField.keyboardType = .asciiCapable
        comfirmPasswordTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        contentView.addSubview(comfirmPasswordTextField)
        comfirmPasswordTextField.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(20)
            make.left.right.equalTo(usernameTextField)
        }
        
        otpTextField.titleAttributedText = "2FA OTP*".attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        contentView.addSubview(otpTextField)
        otpTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        otpTextField.snp.makeConstraints { make in
            make.top.equalTo(comfirmPasswordTextField.snp.bottom).offset(20)
            make.left.right.equalTo(usernameTextField)
        }
        
        confirmButton.addTo(contentView)
        confirmButton.isEnabled = false
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.left.right.equalTo(usernameTextField)
            make.height.equalTo(AppStyle.size.buttonHeight.scaled)
            make.top.equalTo(otpTextField.snp.bottom).offset(30)
        }
    }
    
    func reset() {
        usernameTextField.textField.text = nil
        passwordTextField.textField.text = nil
        comfirmPasswordTextField.textField.text = nil
        otpTextField.textField.text = nil
        textFieldDidChange(usernameTextField.textField)
    }
    
    @objc func textFieldDidChange(_ sender: UITextField) {
        if let username = usernameTextField.textField.text,
           let password = passwordTextField.textField.text,
           let comfirmPassword = comfirmPasswordTextField.textField.text,
           let otp = otpTextField.textField.text,
           isValidUsername(username),
           isValidPassword(password),
           password == comfirmPassword,
           otp.count > 0
        {
            confirmButton.isEnabled = true
        } else {
            confirmButton.isEnabled = false
        }
    }
    
    @objc func confirmButtonTapped() {
        guard let username = usernameTextField.textField.text,
              let password = passwordTextField.textField.text,
              let otf = otpTextField.textField.text else {
            return
        }
        
        HUD.showLoadingInView(self.view)
        MineService.forgetPassword(username: username, password: password, otf: otf) {
            HUD.showMessage("修改成功".localizedString)
            self.navigationController?.popViewController(animated: true)
        } failure: { error in
            
        }
    }

}



 
